package com.performax.service;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
@RequestMapping("/app/api/file")
public class OheGameFileUploadController {
	private static final Logger logger = LoggerFactory.getLogger(OheGameFileUploadController.class);
	
	@RequestMapping(value="/fileUpload")
	@ResponseBody
	public String rolling(@RequestParam("file") MultipartFile file, ModelMap model, HttpSession session) throws Exception {
		
		
		String uploadPath = session.getServletContext().getRealPath("/upload/");
		System.out.println("UPLOAD_PATH : "+uploadPath);
		FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(uploadPath+"/"+file.getOriginalFilename()));
		return "fileupload/upload";
	}
	
	@RequestMapping(value="/mfileupload")
	@ResponseBody
	public Object multiFileUpload(@RequestParam(value="authKey", required=true) 			String authKey
									,@RequestParam(value="mediaCd", defaultValue="mcdoheapp")	String mediaCd
									,@RequestParam(value="userId", required=true) 				String userId
									,@RequestParam(value="gid", defaultValue="XXXXXXXXXX") 		String gid
									,@RequestParam(value="boardType", required=true) 			String boardType
									,@RequestParam(value="title", required=true) 				String title
									,@RequestParam(value="contents", required=true) 			String contents 
									,@RequestParam(value="uploadCnt", required=true) 			int uploadCnt
									,@RequestParam(value="youTubeUrl", required=false) 			String youTubeUrl
									,@RequestParam(value="regType", required=true) 				String regType
									,@RequestParam(value="boardId", required=false) 			int boardId 
									,HttpServletRequest req, ModelMap model, HttpSession session) throws Exception {
		
		HashMap<String, Object>		param	=	new	HashMap<String, Object>();
		param.put("authKey", authKey);
		param.put("mediaCd", mediaCd);
		param.put("userId", userId);
		param.put("gid", gid);
		param.put("boardType", boardType);
		param.put("title", title);
		param.put("contents", contents);
		param.put("uploadCnt", uploadCnt);
		param.put("youTubeUrl", youTubeUrl);
		param.put("regType", regType);
		param.put("boardId", boardId);
		
		
		
		MultipartHttpServletRequest multipartReq	=	(MultipartHttpServletRequest)req;
		//List<MultipartFile> files	=	multipartReq.getFiles("upFile");
		Map<String, MultipartFile>	filemap	=	multipartReq.getFileMap();
		
		
		String uploadPath = session.getServletContext().getRealPath("/upload/");
		
		File	upPath	=	new	File(uploadPath);
		
		if(!upPath.exists()){
			upPath.mkdir();
		}
		HashMap<String, Object>		result	=	new	HashMap<String, Object>();
		//int i = 1;
		
		for(int i = 0 ; i < filemap.size() ; i++){
			MultipartFile file	=	filemap.get("upFile"+(i+1));
			System.out.println("UPLOAD_PATH : "+uploadPath);
			String orgName	=	URLDecoder.decode(new String(file.getOriginalFilename().getBytes()), "UTF-8");
			System.out.println("file name "+orgName);
			FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(uploadPath+"/"+orgName));
			
			result.put("file"+i, uploadPath+"/"+orgName);
			
		}
		result.put("param", param);
		
		
		/*for(MultipartFile file	: files){
			
			System.out.println("UPLOAD_PATH : "+uploadPath);
			String orgName	=	URLDecoder.decode(new String(file.getOriginalFilename().getBytes()), "UTF-8");
			System.out.println("file name "+orgName);
			FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(uploadPath+"/"+orgName));
			
			result.put("file"+i, uploadPath+"/"+orgName);
			i++;
		}*/
		
		return result;
	}
}

